#ifndef DCSPLAYER_H
#define DCSPLAYER_H

#include <QObject>
#include <QTimer>

#include "udpworker.h"
#include "dcsfileoperator.h"
#include "dcsdb/dcsdboperator.h"

#define UDP_FIRST_PAYLOAD_MAX            1472
#define UDP_FOLLOW_PAYLOAD_MAX           1480
#define PROTO_STREAM_FIX_SIZE            20         //视频数据协议字段固定大小
#define STREAM_FIRST_DATA_SIZE           (UDP_FIRST_PAYLOAD_MAX - PROTO_STREAM_FIX_SIZE)
#define STREAM_FOLLOW_DATA_SIZE          (UDP_FOLLOW_PAYLOAD_MAX - PROTO_STREAM_FIX_SIZE)
#define FLOOR_FRAME(size)                ((((size) - 1 ) / (STREAM_FOLLOW_DATA_SIZE) ) + 1)

#define QUINT8_MAX                      255

class DCSPlayer : public QObject
{
Q_OBJECT
public:
    explicit DCSPlayer(QObject *parent = nullptr);

    ~DCSPlayer() override;

    enum PlayState
    {
        Stop = 0x0,
        Play = 0x1,
        Pause = 0x2,
    };

    //播放模式
    enum PlayMode
    {
        SingleCycle = 0x0,
        ListsCycle = 0x1
    };

    quint8 fps() const;

    void setFps(const quint8 &fps);

    quint8 playProgress();

    quint64 duration() const;

    quint8 curVideoIndex() const;

    void setCurVideoIndex(const quint8 &curVideoIndex);

    void incCurVideoIndex();

    void decCurVideoIndex();

    QList<QString> playLists() const;

    void setPlayLists(const QList<QString> &playLists);

    quint8 state() const;

    void setState(const quint8 &state);

    quint8 mode() const;

    void setMode(const quint8 &mode);

    quint64 curFrameNo() const;

    void setCurFrameNo(const quint64 &curFrameNo);

    void videoPrepare();

    void videoCleanup();

    void playerStart();

    void playerStop();

    void playerPause();

    void playerContinue();

    void playerNext();

    void playerPrev();

    QString getCurrentVideoName();

signals:

    void playStateChanged(quint8 state);

    void playVideoPrepared(quint8 index);

public slots:

    void tickhandler();

private:
    UdpWorker *mUdp;
    DCSFileOperator *mFopt{};
    DCSDbOperator *mDbopt;
    QList<QString> mPlayLists;       /*播放节目列表*/
    quint8 mState;              /*播放状态*/
    quint8 mMode;                /*播放循环模式*/
    quint8 mFps;                        /*播放帧率*/
    QTimer *mFrameTimer;                /*帧定时器*/
    quint8 mPlayProgress;               /*播放进度*/
    quint64 mDuration;                  /*节目时长(ms)*/
    quint8 mCurVideoIndex;              /*当前节目序号*/
    quint64 mCurFrameNo;                /*当前帧号*/
    quint64 mCurVideoFrameNum{};          /*当前节目总帧数*/
};

#endif // DCSPLAYER_H
